<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
use app\common\model\Stat;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Index extends AdminBase
{
    protected $stat;
    protected function initialize()
    {
        $this->stat = new Stat();
        parent::initialize();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {

        $total_user = $this->stat->sum("new_users");// 总注册用户数
        $total_roles = $this->stat->sum("new_roles");// 总注册角色数
        $total_amount = $this->stat->sum("pay_amount");// 总充值金额
        // 总计数据
        $total = [
            'new_users' => $total_user ?? 0,
            'new_roles' => $total_roles ?? 0,
            'pay_amount' => $total_amount ?? 0,
        ];
        $this->assign('total', $total);

        $today_date = date('Y-m-d', time());
        $today_data = $this->stat->getStatDayList(['stat_date' => $today_date])->toArray();

        $today_data = $today_data['data'][0] ?? [];

        // 今日数据
        $today = [
            'new_users' => $today_data['new_users'] ?? 0,
            'new_roles' => $today_data['new_roles'] ?? 0,
            'active_users' => $today_data['active_users'] ?? 0,
            'pay_amount' => $today_data['pay_amount'] ?? 0,
            'payment_rate' => !empty($today_data['active_users']) ? round($today_data['pay_users']/$today_data['active_users'],2) * 100 : 0,// 付费率 当天付费人数/当天活跃人数
            'arpu' => !empty($today_data['pay_amount']) ?  round($today_data['pay_amount']/$today_data['active_users'],2) * 100 :0,// arpu 当天充值金额/当天活跃人数
        ];
        $this->assign('today', $today);

        // 游戏排行
        $where[] = ['stat_date','=',$today_date];
        $where[] = ['gid','>',0];
        $top_games = $this->stat->getStatGameList($where,'new_roles desc');
        foreach ($top_games as &$val){
            $val['name'] = get_game_filed($val['gid'],'name');
            $val['payment_rate'] = $val['active_users'] ? round($val['pay_users']/$val['active_users'],2) * 100 : 0;// 付费率
        }
        $this->assign('top_games', $top_games);

        // 7日趋势数据
        $star_date = date('Y-m-d', strtotime("-6 day"));

        $seven_map[] = ['stat_date','between time',[$star_date,$today_date]];

        $seven_data = $this->stat->getDayList($seven_map);
        $seven_days = [];
        foreach ($seven_data as $vals){
            $seven_days['date'][] = $vals['stat_date'];
            $seven_days['new_users'][] = $vals['new_users'];
            $seven_days['new_roles'][] = $vals['new_roles'];
            $seven_days['payment'][] = $vals['pay_amount'];
        }

        $this->assign('seven_days', $seven_days);

        // 30日趋势数据
        $star_date = date('Y-m-d', strtotime("-29 day"));

        $thirty_map[] = ['stat_date','between time',[$star_date,$today_date]];
        $thirty_data = $this->stat->getDayList($thirty_map);

        $thirty_days = [];
        foreach ($thirty_data as $vals){
            $thirty_days['date'][] = date('n-j', strtotime($vals['stat_date']));
            $thirty_days['new_users'][] = $vals['new_users'];
            $thirty_days['new_roles'][] = $vals['new_roles'];
            $thirty_days['payment'][] = $vals['pay_amount'];
        }

//        $thirty_days = [
//            'date' => ['1日', '2日', '3日', '4日', '5日', '6日', '今日'],
//            'new_users' => [80, 85, 90, 92, 95, 98, 100],
//            'new_roles' => [120, 125, 130, 135, 140, 145, 150],
//            'payment' => [20000, 21000, 22000, 23000, 24000, 24500, 25000]
//        ];
        $this->assign('thirty_days', $thirty_days);
        return $this->fetch('index');
    }



    public function gameRank()
    {
        // 获取排序参数
        $field = input('get.sort', 'new_users'); // 默认按新增用户排序
        $today_date = date('Y-m-d', time());
        // 游戏排行
        $topGames = $this->stat->getStatGameList(['stat_date' => $today_date],$field.' desc');
        foreach ($topGames as &$val){
            $val['name'] = get_game_filed($val['gid'],'name');
            $val['payment_rate'] = !empty($val['active_users']) ? round(($val['pay_users']/$val['active_users']) * 100,2) : 0;// 付费率
        }
        // 如果是AJAX请求
        return json([
            'code' => 0,
            'data' => $topGames,
            'msg' => 'success'
        ]);

    }
}